package com.zqwh.xtool.model.offline;

import android.content.Context;

import com.zqwh.yj.sdk.utils.LogUtils;
import com.zqwh.yj.sdk.utils.httpRequest.subscribers.NetCallback;
import com.zqwh.xtool.api.offline.UserResposity;
import com.zqwh.xtool.converter.User2UserTemplatesVOConverter;
import com.zqwh.xtool.converter.User2UserVOConverter;
import com.zqwh.xtool.domain.UserTemplatesVO;
import com.zqwh.xtool.domain.UserVO;
import com.zqwh.xtool.greendao.db.User;
import com.zqwh.xtool.model.UserModel;

import java.util.List;

public class UserModelOfflineImpl implements UserModel {
    private UserResposity userResposity;
    private Context context;

    public UserModelOfflineImpl(Context context) {
        this.context = context;
        this.userResposity = new UserResposity(this.context);
    }

    @Override
    public void signInByAccount(Long devId, String username, String password, NetCallback<UserVO> callback) {
        List<User> userList = userResposity.queryUserByAccountAndPwd(username, password);

        if (userList == null || userList.size() == 0){
            callback.onError("账号或密码错误");
        }else {
            LogUtils.logout("Get user form db : "+userList.toString());
            callback.onSuccess(User2UserVOConverter.convert(userList.get(0)));
        }
    }

    @Override
    public void signInByCardNo(Long devId, String cardNo, NetCallback<UserVO> callback) {
        User user = userResposity.queryUserByCardNo(cardNo);

        if (user == null){
            callback.onError("IC卡未激活");
        }else {
            callback.onSuccess(User2UserVOConverter.convert(user));
        }
    }

    @Override
    public void signInById(Long devId, Long userId, NetCallback<UserVO> callback) {
        User user = userResposity.queryUserByUserId(userId);

        if (user == null){
            callback.onError("用户不存在");
        }else {
            callback.onSuccess(User2UserVOConverter.convert(user));
        }
    }

    @Override
    public void getTemplateList(NetCallback<List<UserTemplatesVO>> callback) {
        List<User> userList = userResposity.queryAllUser();

        callback.onSuccess(User2UserTemplatesVOConverter.convert(userList));
    }

    @Override
    public void setTemplate(Long userId, Integer templateNum, String template, NetCallback<Object> callback) {
        User user = userResposity.queryUserByUserId(userId);
        if (templateNum == 1){
            user.setTemplate_1(template);
        }else if (templateNum == 2){
            user.setTemplate_2(template);
        }else if (templateNum == 3){
            user.setTemplate_3(template);
        }else if (templateNum == 4){
            user.setTemplate_4(template);
        }else {
            callback.onError("错误的指静脉编号");
        }

        userResposity.updateUser(user);
        callback.onSuccess("访问成功");
    }

    @Override
    public boolean insertMultUser(List<User> userList) {
        return userResposity.insertMultUser(userList);
    }

    @Override
    public List<User> queryAllUser() {
        return userResposity.queryAllUser();
    }
}
